package com.lk.api.own;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;

import com.lk.api.R;

public class MyMenu extends ListActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Button b = (Button) findViewById(R.id.menuButton);
		/* 给View 的子控件绑定ContextMenu 菜单，长按此子控件就会弹出ContextMenu菜单 */

		String[] mItemName = new String[] { "李四", "王五", "小三", "张三" };
		ArrayAdapter<String> adapterView = new ArrayAdapter<String>(
				MyMenu.this, android.R.layout.simple_expandable_list_item_1,
				mItemName);
		setListAdapter(adapterView);
		registerForContextMenu(getListView());


		Log.d("onCreate", "onCreate");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		Log.d("onResume", "onResume");
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.d("onDestroy", "onDestroy");
		super.onDestroy();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		Log.d("onStart", "onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		Log.d("onStop", "onStop");
		super.onStop();
	}

	// 第一次打开activity触发
	/* 第一次按点击手机上界面上的菜单键，会调用此方法，只方法只调用一次 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		System.out.println("onCreateOptionsMenu");
//		getMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);

		// 动态加载菜单
		// menu.add("save");
		// menu.add("del");
		// menu.add("upd");
		// menu.add("sel");
		//
		// return super.onCreateOptionsMenu(menu);

		// 通过src 资源文件加载
		// getMenuInflater().inflate(R.menu.main, menu);
		// return true;
	}

	// 第一次打开activity触发
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		System.out.println("onPrepareOptionsMenu");
		return super.onPrepareOptionsMenu(menu);
	}

	/* 每次长按ContextMenu被绑定的View的子控件，都会调用此方法 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		System.out.println("onCreateContextMenu");
		menu.add(1,1,1,"save");
		menu.add(1,2,2,"del");
		menu.add(1,3,3,"upd");
		menu.add(1,4,4,"sel");
		getMenuInflater().inflate(R.menu.main, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	// 长按弹出菜单 点击触发
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		System.out.println("onContextItemSelected" +" info:"+ info.position+" item:"+item.getItemId());
		return super.onContextItemSelected(item);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		System.out.println("onOptionsItemSelected" + item.getItemId());
		return true;
	}

}
